/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package stm_database;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.PrintStream;
import java.util.ArrayList;

/**
 *
 * @author stepj5am
 */
public class DatabaseController {
    
    /**
     * nactena databaze
     */
    protected Database data;
    /**
     * prompt na prikazovou radku
     */
    protected String prompt = ">";
    public DatabaseController()
    {
        
    }
    public DatabaseController(Database d)
    {
        data = d;
    }
    /**
     * uzivatelsky vstup
     * @param in input stream
     * @param out ouput stream
     */
    public void start(BufferedReader in, PrintStream out)
    {
        String line = "";
        while(true)//cekam na vstup uzivatele
        {
            out.print(prompt);
            try
            {
                line = in.readLine();
            }catch(IOException e)
            {
                out.println(e.getMessage());
                continue;
            }
            //interakce s uzivatelem
            String[] words = line.split("\\s+");
            if(words.length == 0 )
                continue;//prazdna radka neudela nic
            if(words.length > 3)
            {
                out.println("prilis mnoho argumentu");
                continue;
            }
            if(words[0].equals("new"))
            {
                if(words.length != 2)
                {
                    out.println("zadejte prikaz new se jmeny sloupcu oddelenymi stredniky");
                    continue;
                }
                data = new Database(words[1].split(DatabaseItem.delimiter));
                out.println("databaze vytvorena");
                continue;
            }
            if(words[0].equals("add"))
            {
                if(words.length != 2 )
                {
                    out.println("chybi jmeno souboru s databazi");
                    continue;
                }
                try
                {
                    data.add_item(words[1].split(DatabaseItem.delimiter,data.column_number()));
                }catch(IOException e)
                {
                    out.println(e.getMessage());
                }
                continue;
            }
            if(words[0].equals("open"))
            {
                if(words.length != 2 )
                {
                    out.println("chybi jmeno souboru s databazi");
                    continue;
                }
                try
                {
                    data = new Database(words[1]);
                }catch(IOException e)
                {
                    out.println(e.getMessage()); 
                    continue;
                }
                out.printf("databaze %s uspesne nactena\n", words[1]);
                continue;
            }
            if(words[0].equals("save"))
            {
                String save = null;
                if(words.length > 1)
                    save = words[1];
                try
                {
                    data.save(save);
                }catch(IOException e)
                {
                    out.println(e.getMessage());                    
                }
                continue;
            }
            if(words[0].equals("delete"))
            {
                if(words.length != 2)
                {
                    out.println("chybny pocet parametru");
                    continue;
                }
                try
                {
                    data.delete_item(words[1]);
                }catch(BadCommandParameterException e)
                {
                    out.println(e.getMessage());
                    
                }
                continue;
            }
            if(words[0].equals("edit"))
            {
                if(words.length != 3)
                {
                    out.println("chybny pocet parametru");
                    continue;
                }
                try
                {
                    data.edit_item(words[1], words[2].split(DatabaseItem.delimiter,data.column_number()));
                    out.println("polozka zmenena");
                    
                }catch(IOException e)
                {
                    out.println(e.getMessage());                    
                }
                continue;
            }   
            if(words[0].equals("sort"))
            {
                if(words.length != 2)
                {
                    out.println("chybny pocet parametru");
                    continue;
                }
                try
                {
                    data.sort(words[1]);
                }catch(IOException e)
                {
                    out.println(e.getMessage());
                }
                continue;
            }
            if(words[0].equals("find"))
            {
                if(words.length != 2)
                {
                    out.println("chybny pocet parametru");
                    continue;
                }
                try
                {
                    DatabaseItem[] result = data.find(words[1].split(DatabaseItem.delimiter,data.column_number()));                
                    if(result!= null)
                        for(DatabaseItem i : result)
                        {
                            out.println(i.toString());
                        }
                }catch(IOException e)
                {
                    out.println(e.getMessage());
                }
                
                continue;
            }
            if(words[0].equals("list"))
            {
                if(words.length == 1)
                    out.print(data.print(0, data.size()));
                else
                {
                    int records_per_page = 0;
                    try
                    {
                        records_per_page = Integer.parseInt(words[1]);
                    }catch(NumberFormatException e)
                    {
                        out.println("spatny parametr prikazu");
                        continue;
                    }
                    int current = 0;
                    if(records_per_page <=0)
                    {
                        out.println("zadejte parametr vetsi nez jedna");
                        continue;
                    }
                    while(true)//ceka na vstup uzivatele
                    {
                        if (current < 0)
                            current = 0;
                        if(current >= data.size())
                            break;
                        out.println(data.print(current, current+records_per_page));
                        try
                        {
                            line = in.readLine();
                        }catch(IOException e)
                        {
                            out.println(e.getMessage());
                        }
                        if(line.equals("n"))
                        {
                            current += records_per_page;
                        }
                        if(line.equals("p"))
                        {
                            current -= records_per_page;
                        }
                        if(line.equals("end"))
                        {
                            break;
                        }
                        out.println("neznamy prikaz");
                        
                    }
                }
                out.println();
                continue;
            }
            if(words[0].equals("help"))
            {
                out.println("napoveda k programu");
                out.println("seznam prikazu s parametry:");
                out.println("open soubor : otevre databazi ulozenou v souboru");
                out.println("save [soubor]: ulozi databazi prepsanim stavajiciho, nebo vytvorenim noveho souboru");
                out.println("add polozka: prida polozku do databaze");
                out.println("delete id_polozky: smaze polozku s id_polozky");
                out.println("edit id nova_polozka: nahradi polozku s id novou_polozkou");
                out.println("sort key: setridi podle key vzestupne, pokud uz je setrizeno vzestupne, tak setridi sestupne");
                out.println("find maska_polozky: najde polozky podle masky");
                out.println("list [pocet_zobrazenych_polozek]: zobrazi vsechny polozky, nebo listuje po poctu_zobrazenych polozek");
                continue;
            }
           if(words[0].equals("end"))
               break;
           //prikaz neni mezi platnymi
           out.println("neznamy prikaz");
           continue;
        }
    }

}
